package com.solocredit.finance.ui.splash.impl

import android.content.Context
import com.solocredit.finance.http.IHttpCallback
import com.solocredit.finance.mvp.presenter.MvpPresenter
import com.solocredit.finance.ui.splash.view.IMullaSplashView
import com.solocredit.finance.utils.helper.SharedHelper
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/7/25 13:46
 * @Annotation：启动页 Presenter 实现
 */
class MullaSplashPresenterImpl(context: Context?) : MvpPresenter<IMullaSplashView>(context) {

    private var model: MullaSplashModelImpl? = null
    init {
        model = MullaSplashModelImpl()
    }

    /**
     * 获取地区转发
     */
    fun loadMullaTimeZoneAction(){
        model?.loadMullaTimeZoneAction(object : IHttpCallback {
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val dataString = data.optString("data","")
                        mvpView?.showMullaTimeZoneRequestData(dataString)
                    }
                    else -> {
                        val msg = data.optString("msg")
                        mvpView?.showMullaToast(msg)
                        mvpView?.hideMullaLoading()
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
                mvpView?.hideMullaLoading()
                //是否是网络异常
                val errorCode = error.optInt("errorCode",0)
                if (isNetworkError(errorCode)){
                    mvpView?.showMullaNetworkErrorDialog("Launch Page Get TimeZone", onRetryClick = {
                        mvpView?.showMullaLoading()
                        loadMullaTimeZoneAction()
                    })
                    return
                }
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }
            override fun onRequestFinish() {
            }
        })
    }

    /**
     * 获取App统一配置信息
     */
    fun loadMullaCommonConfigAction(){
        model?.loadMullaCommonConfigAction(object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        mvpView?.hideMullaLoading()
                        val obj = data.optJSONObject("obj")
                        //保存所有数据
                        if (obj != null) {
//                            // TODO: 测试模拟版本更新
//                            val customize = obj.optJSONObject("customize")
//                            val customData = customize.optJSONObject("customData")
//                            val testVersion = JSONObject()
//                            testVersion.put("isMustUpdate",true)
//                            testVersion.put("versionCode","18")
//                            testVersion.put("versionName","1.0.18")
//                            testVersion.put("versionRemark","1.A brand new UI design.$2.Update more secure data processing.$3.Resolve known bugs, optimize code, and enhance user experience.")
//                            customData.put("release",testVersion)
                            SharedHelper.saveMullaSPData(SharedHelper.MULLA_SPKEY.MULLA_COMMON,obj.toString())
                        }
                        mvpView?.showMullaConfigSuccess()
                    }

                    else -> {
                        val msg = data.optString("msg")
                        mvpView?.showMullaToast(msg)
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                //是否是网络异常
                val errorCode = error.optInt("errorCode",0)
                if (isNetworkError(errorCode)){
                    mvpView?.showMullaNetworkErrorDialog("Launch Page Get AppShowInfo", onRetryClick = {
                        mvpView?.showMullaLoading()
                        loadMullaCommonConfigAction()
                    })
                    return
                }
                mvpView?.showMullaToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideMullaLoading()
            }
        })
    }
}